<template>
	<div class="front-padding" v-loading="loading[0]" element-loading-text="拼命加载中..." element-loading-spinner="el-icon-loading" element-loading-background="#fff">
		<div class="widgets bg-white">
			<div class="btn-group right">
				<el-button type="info" @click="refresh">刷新</el-button>
				<el-button type="primary" v-if="isAdd" @click="add">新增</el-button>
				<el-button type="primary" v-else disabled>新增</el-button>
				<el-button type="success" v-if="isEdit" @click="edit">编辑</el-button>
				<el-button type="success" v-else disabled>编辑</el-button>
				<el-button type="danger" v-if="isEdit" @click="del">删除</el-button>
				<el-button type="danger" v-else disabled>删除</el-button>
			</div>
		</div>
		<div class="tree bg-white bj">
			<el-scrollbar style="height: 100%;">
				<el-tree :data="treeData" :props="defaultProps" node-key="id" :default-expanded-keys="dfExpKeys" ref="tree" highlight-current accordion @node-click="nodeClick" :default-expand-all="false" :expand-on-click-node="false"></el-tree>
			</el-scrollbar>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			loading: this.$store.state.accountSubjectStore.loading,
			treeData: this.$store.state.accountSubjectStore.groupTreeData,
			defaultProps: {
				label: 'name',
                children: 'subAssertList'
			},
			dfExpKeys: [],
			isAdd: false,		//是否可新增
			isEdit: false,		//是否可编辑
		}
	},
	watch: {
		treeData(newVal) {
			this.$nextTick(() => {
				this.$refs.tree.setCurrentKey(newVal[0]['id']);			//设置高亮状态
				this.dfExpKeys = [newVal[0]['id']];
			});
		}
	},
	methods: {
		nodeClick(row) {
			this.$store.commit('accountSubjectStore/updateGroupNode', row);
			let code = row['code'].split('.');
			row['code'] === '101' ? this.isAdd = false : this.isAdd = true;		//会计科目节点不能新增
			code[0] === '101' ? this.isEdit = false : this.isEdit = true;		//会计科目以及7大类不可编辑
		},
		refresh() {			//刷新
			this.$store.dispatch('accountSubjectStore/getGroupTreeData');
		},
		add() {		//新增
			this.$store.commit('accountSubjectStore/updateGroupStatus', 0);
			this.$util.openLayerIframe({title: '增加目录', zIndex: 13, width: '420px', height: '240px', url: this.$domain + '/#/basicData/accountSubject/subjectGroup/subjectGroupForm'});
		},
		edit() {	//编辑
			this.$store.commit('accountSubjectStore/updateGroupStatus', 1);
			this.$util.openLayerIframe({title: '编辑目录', zIndex: 13, width: '420px', height: '240px', url: this.$domain + '/#/basicData/accountSubject/subjectGroup/subjectGroupForm'});
		},
		del() {
			/*
			*	1、有子节点不能删除
			*	2、7大类不能删除
			*	3、编码为4位的不能删除
			*/
            let groupNode = this.$store.state.accountSubjectStore.groupNode;
            if(groupNode['subAssertList'].length > 0) {
                this.$message.warning({message: '需要先删除的节点有子节点', duration: 1000, type: 'warning'});
            } else {
                let code = groupNode['code'].split('.')[0];
                if(code == 101 || code.length == 4) {
                    this.$message.warning({message: '不能删除该节点', duration: 1000, type: 'warning'});
                } else {
                    this.$confirm('删除数据不可恢复, 确定要删除选中的节点吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.$http.get('subjectGroup/deleteGroupById?id=' + groupNode['id']).then(res => {
                            if(res.data.code == 20001) {
                            	this.$message.success({message: '删除成功', duration: 1000, type: 'success'});
                                setTimeout(() => {
                                	this.$store.dispatch('accountSubjectStore/getGroupTreeData');
	                                this.$store.dispatch('accountSubjectStore/getTreeData');
                                }, 1000);
                                this.isAdd = false;
                                this.isEdit = false;
                            } else {
                                this.$message.error({message: res.data.msg, duration: 1000, type: 'error'});
                            }
                        });
                    }).catch(() => {
                        this.$message.warning({message: '已取消删除', duration: 1000, type: 'warning'});
                    });
                }
            }
		}
	},
	created() {
		this.$store.dispatch('accountSubjectStore/getGroupTreeData');
	}
}
</script>
<style scoped>
	.tree {height: calc(100% - 54px);box-sizing: border-box;padding: 5px 0;}
	.widgets {min-width: auto;}
</style>